package com.cyan.app.workflow.activity.LGS;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.cyan.app.barcode.CaptureActivity;
import com.cyan.app.workflow.R;
import com.cyan.app.workflow.base.activity.BaseActivity;
import com.cyan.app.workflow.bean.CommonBean;
import com.cyan.app.workflow.bean.DelPoolListBean;
import com.cyan.app.workflow.common.HttpConfig;
import com.cyan.app.workflow.customview.ShowTipDialog;
import com.cyan.app.workflow.util.AppUtils;
import com.cyan.app.workflow.util.network.GetRequestParams;
import com.google.gson.Gson;

import org.json.JSONObject;

/**
 * Created by DXW on 2017-07-18.
 */

public class DellzsActivity extends BaseActivity {
    private TextView mTitleName;
    private Button mTitleSeach;
    private View mTitleView;
    private EditText mEditTextUser;
    private EditText mEditTextChqy;
    private Button mButtonUser;
    private Button mButtonSubmit;
    private DelPoolListBean.DelpoolBean delpoolBean;
    private static final String USERNO = "DEL_LZS_USERNO";
    private ShowTipDialog showdialog = new ShowTipDialog();
    private EditText mEditTextChqy2;
    private TextView mTextViewChqy2_U;
    private TextView mTextViewChqy1_U;
    private View lineChqy2;
    private Double susqy1 = 0.00;
    private Double susqy2 = 0.00;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lgs_del_lzs);
        titleBar();
        initView();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 0x0001 && resultCode == RESULT_OK) {
            String result = "";
            if(data != null) {
                result = data.getStringExtra("result");
            }
            if (!result.contains("40010")) {
                AppUtils.showTip(DellzsActivity.this, "员工不存在");
            }else {
                mEditTextUser.setText(result.substring(5));
                PreferenceManager.getDefaultSharedPreferences(this).edit().putString(USERNO,result.substring(5)).commit();
            }
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //MobclickAgent.onPause(this);
        //MobclickAgent.onPageEnd("DDActivity");
    }

    @Override
    protected void onResume() {
        super.onResume();
        //MobclickAgent.onResume(this);
        //MobclickAgent.onPageStart("DDActivity");
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //DDDataFetcher.getInstance().unRegistPoolsObserver(this);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    private void titleBar() {
        mTitleName = (TextView) findViewById(R.id.top_title_info);
        mTitleName.setText("送检");
        Button back = (Button) findViewById(R.id.top_title_back);
        back.getBackground().setAlpha(255);// 透明度0~255透明度值 ，值越小越透明
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //返回按钮
                finish();
            }
        });
        mTitleSeach = (Button) findViewById(R.id.top_title_refres_with_icon);
        mTitleSeach.setVisibility(View.INVISIBLE);
        mTitleSeach = (Button) findViewById(R.id.top_title_refres);
        mTitleSeach.setVisibility(View.INVISIBLE);
        mTitleSeach.setText("筛选");
        mTitleSeach.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        mTitleView = (View) findViewById(R.id.layout_main_ui_top2_title);
    }
    private void initView() {
        delpoolBean = (DelPoolListBean.DelpoolBean) getIntent().getSerializableExtra("delpoolDetail");
        mEditTextChqy = (EditText)findViewById(R.id.lgs_del_lzs_accqy1);
        if(delpoolBean.getChqy1()>0&&(delpoolBean.getBckqy1()>0||delpoolBean.getWahqy()>0)){
            susqy1 = delpoolBean.getChqy1() - delpoolBean.getBckqy1()-delpoolBean.getWahqy()>0?delpoolBean.getChqy1() - delpoolBean.getBckqy1()-delpoolBean.getWahqy():0 ;
            mEditTextChqy.setInputType(InputType.TYPE_NULL);
        }else {
            //susqy1 = delpoolBean.getAccqy1() - delpoolBean.getBckqy1()-delpoolBean.getWahqy()>0?delpoolBean.getAccqy1() - delpoolBean.getBckqy1()-delpoolBean.getWahqy():0 ;
            susqy1 = delpoolBean.getAccqy1();
        }
        mEditTextChqy.setText(String.valueOf(susqy1));
        mTextViewChqy1_U = (TextView) findViewById(R.id.lgs_del_lzs_accqy1_u);
        mTextViewChqy1_U.setText(delpoolBean.getUnmsr1());
        if(delpoolBean.getDeltype()==0&&delpoolBean.getUnmsr2() != null && delpoolBean.getUnmsr2().length()>0){
            mEditTextChqy2 = (EditText)findViewById(R.id.lgs_del_lzs_accqy2);
            if(delpoolBean.getChqy2()>0&&(delpoolBean.getBckqy2()>0||delpoolBean.getWahqy2()>0)){
                susqy2 = delpoolBean.getChqy2() - delpoolBean.getBckqy2()-delpoolBean.getWahqy2()>0?delpoolBean.getChqy2() - delpoolBean.getBckqy2()-delpoolBean.getWahqy2():0 ;
                mEditTextChqy2.setInputType(InputType.TYPE_NULL);
            }else {
                //susqy2 = delpoolBean.getAccqy2() - delpoolBean.getBckqy2()-delpoolBean.getWahqy2()>0?delpoolBean.getAccqy2() - delpoolBean.getBckqy2()-delpoolBean.getWahqy2():0 ;
                susqy2 = delpoolBean.getAccqy2();
            }
            mEditTextChqy2.setText(String.valueOf(susqy2));
            mTextViewChqy2_U = (TextView) findViewById(R.id.lgs_del_lzs_accqy2_u);
            mTextViewChqy2_U.setText(delpoolBean.getUnmsr2());
        }else{
            lineChqy2=(View) findViewById(R.id.lgs_del_lzs_line_qty2);
            lineChqy2.setVisibility(View.GONE);
        }
        mEditTextUser=(EditText)findViewById(R.id.lgs_del_lzs_ygk);
        mEditTextUser.setInputType(InputType.TYPE_NULL);
        String ygbh = PreferenceManager.getDefaultSharedPreferences(this).getString(USERNO,null);
        if(ygbh!=null){
            mEditTextUser.setText(String.valueOf(ygbh));
        }
        mButtonUser =(Button)findViewById(R.id.lgs_del_lzs_ygk_sm);
        mButtonUser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(DellzsActivity.this, CaptureActivity.class), 0x0001);
            }
        });
        mButtonSubmit = (Button)findViewById(R.id.lgs_del_lzs_submit);
        mButtonSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) { //送检
                if (mEditTextChqy.getText().length() == 0||Double.parseDouble(mEditTextChqy.getText().toString())<= 0) {
                    AppUtils.showTip(DellzsActivity.this, "点收数量1必须大于0");
                    return;
                }
                if (Double.parseDouble(mEditTextChqy.getText().toString())> susqy1) {
                    AppUtils.showTip(DellzsActivity.this, "点收数量1不能大于待处理数，待处理数=送货数(点收数)-流转数-退回数");
                    return;
                }
                if (mEditTextUser.getText().toString().isEmpty()) {
                    AppUtils.showTip(DellzsActivity.this, "送检人员不能为空");
                    return;
                }
                if(delpoolBean.getDeltype()==0&&delpoolBean.getUnmsr2() != null&&delpoolBean.getUnmsr2().length()>0&&(mEditTextChqy2.getText().length() == 0|| Double.parseDouble(mEditTextChqy2.getText().toString())<= 0)) {
                    AppUtils.showTip(DellzsActivity.this, "点收数量2必须大于0");
                    return;
                }
                if(delpoolBean.getDeltype()==0&&delpoolBean.getUnmsr2() != null&&delpoolBean.getUnmsr2().length()>0&&Double.parseDouble(mEditTextChqy2.getText().toString())> susqy2) {
                    AppUtils.showTip(DellzsActivity.this, "点收数量2不能大于待处理数，待处理数=送货数(点收数)-流转数-退回数");
                    return;
                }
                double chqy2 = 0;
                if(delpoolBean.getUnmsr2() != null&&delpoolBean.getUnmsr2().length()>0){
                    chqy2 = Double.parseDouble(mEditTextChqy2.getText().toString());
                }
                new submitAsyncLoader().execute(mEditTextChqy.getText().toString(),String.valueOf(chqy2),mEditTextUser.getText().toString());
            }
        });
    }

    public class submitAsyncLoader extends AsyncTask<String, Integer, String> {
        @Override
        protected void onPreExecute() {
            showdialog.showDialog(DellzsActivity.this);
        }

        @Override
        protected String doInBackground(String... params) {
            String result = null;
            String action ="509";
            try{
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("poolno", delpoolBean.getPoolno());
                jsonObject.put("chqy1",params[0]);
                jsonObject.put("chqy2",params[1]);
                jsonObject.put("userno", params[2]);
                result= GetRequestParams.getRequestResult(HttpConfig.HTTP_MAIN_SERVER_URL, action, jsonObject.toString(), getApplicationContext());
            }catch (Exception e){
                Log.e("error", e.toString());
            }
            return result;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            if (s == null||s.isEmpty()||s.contains("html")) {
                AppUtils.showTip(DellzsActivity.this,"操作异常，请稍后再试！");
                showdialog.closeDialog();
                return;
            }
            CommonBean commonBean = new Gson().fromJson(s, CommonBean.class);
            if (commonBean.getCode()==1) {
                Intent intent = new Intent();
                intent.putExtra("poolno",delpoolBean.getPoolno());
                DellzsActivity.this.setResult(RESULT_OK,intent);
                AppUtils.showTip(DellzsActivity.this,"保存成功。");
                showdialog.closeDialog();
                finish();
            }else {
                DellzsActivity.this.setResult(RESULT_CANCELED);
                AppUtils.showTip(DellzsActivity.this,"保存失败，"+commonBean.getMsg());
                showdialog.closeDialog();
            }
        }

        @Override
        protected void onCancelled(String s) {
            DellzsActivity.this.setResult(RESULT_CANCELED);
        }
    }
}
